<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/importer/gcloud_trace/gcloud_trace_importer.html">
<link rel="import" href="/tracing/extras/importer/trace_event_importer.html">
<link rel="import" href="/tracing/model/model.html">

<script>

'use strict';

tr.b.unittest.testSuite(function() {
  const GcloudTraceImporter = tr.e.importer.gcloud_trace.GcloudTraceImporter;

  test('noSpans', function() {
    const trace = {projectId: 'My Project', traceId: '123', spans: []};

    const model = new tr.Model();
    const importer = new tr.e.importer.gcloud_trace.GcloudTraceImporter(model,
        JSON.stringify(trace));
    const subtraces = importer.extractSubtraces();
    assert.strictEqual(1, subtraces.length);
    // Note there are, in fact, use cases for traces with no events (spans).
    assert.deepEqual([], subtraces[0].traceEvents);
  });

  test('typicalTrace', function() {
    const span1 = {
      'spanId': '1',
      'kind': 'RPC_CLIENT',
      'name': '/first',
      'startTime': '2015-09-03T16:40:00.841654024Z',
      'endTime': '2015-09-03T16:40:00.856599389Z',
      'labels': {
        'key1': 'value1',
        'key2': 'value2'
      }
    };
    const span2 = {
      'spanId': '2',
      'kind': 'RPC_SERVER',
      'name': '/second',
      'startTime': '2015-09-03T16:40:00.842880028Z',
      'endTime': '2015-09-03T16:40:00.851729538Z',
      'parentSpanId': '1',
      'labels': {
        'key1': 'value3',
        'key2': 'value4'
      }
    };
    const trace = {projectId: 'My Project', traceId: '123',
      spans: [span1, span2]};

    const model = new tr.Model();
    const importer = new tr.e.importer.gcloud_trace.GcloudTraceImporter(model,
        JSON.stringify(trace));
    const subtraces = importer.extractSubtraces();
    assert.strictEqual(1, subtraces.length);
    assert.strictEqual(2, subtraces[0].traceEvents.length);

    const span1Event = subtraces[0].traceEvents[0];
    assert.strictEqual('tracespan', span1Event.cat);
    assert.strictEqual('/first', span1Event.name);
    assert.strictEqual(1441298400841000, span1Event.ts);
    assert.strictEqual(15000, span1Event.dur);
    assert.strictEqual('123', span1Event.pid);
    assert.strictEqual('123', span1Event.tid);
    assert.strictEqual(
        '2015-09-03T16:40:00.856599389Z', span1Event.args['End Time']);
    assert.strictEqual('1', span1Event.args['Span ID']);
    assert.strictEqual('value1', span1Event.args.key1);
    assert.strictEqual('value2', span1Event.args.key2);
    assert.strictEqual(
        '2015-09-03T16:40:00.856599389Z', span1Event.args['End Time']);

    const span2Event = subtraces[0].traceEvents[1];
    assert.strictEqual('tracespan', span2Event.cat);
    assert.strictEqual('/second', span2Event.name);
    assert.strictEqual(1441298400842000, span2Event.ts);
    assert.strictEqual(9000, span2Event.dur);
    assert.strictEqual('123', span2Event.pid);
    assert.strictEqual('123', span2Event.tid);
  });

  test('canImport', function() {
    assert.isTrue(GcloudTraceImporter.canImport(
        JSON.stringify({projectId: 'My Project'})));
    assert.isTrue(GcloudTraceImporter.canImport(
        JSON.stringify({projectId: '56', traceId: '34'})));
    assert.isFalse(GcloudTraceImporter.canImport(
        JSON.stringify({wrongjson: '33'})));
    assert.isFalse(GcloudTraceImporter.canImport(''));
    assert.isFalse(GcloudTraceImporter.canImport([]));
  });
});
</script>
